﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.HVAC;
using YArchitech.Revit;
using YRevitJig;

namespace YArchitech.Plumbing
{
	public class RefrigerantPipeCreateJip : PELineJig
	{
		public RefrigerantPipeCreateJip(UIDocument uiDoc, bool gasLiquidIntegration, List<double> offset, FormRefrigerantPipeCreate refirgerantPipe) : base(uiDoc, refirgerantPipe)
		{
			base.JigStage = PELineJig.EStage.eNoOper;
			this.offset = offset;
			this.gasLiquidIntegration = gasLiquidIntegration;
			base.ResetPoint = XUiJigBase.EResetPoint.eByAxis;
			this.m_UserForm = refirgerantPipe;
			this.OnMouseLeaveFormClientEvent = (WndMsgManager.MouseLeaveFormClientEvnetHandler)Delegate.Combine(this.OnMouseLeaveFormClientEvent, new WndMsgManager.MouseLeaveFormClientEvnetHandler(this.OnMouseLeaveFormClientEvnetHandler));
			this.OnMouseMoveIntoFormClientEvent = (WndMsgManager.MouseMoveIntoFormClientEvnetHandler)Delegate.Combine(this.OnMouseMoveIntoFormClientEvent, new WndMsgManager.MouseMoveIntoFormClientEvnetHandler(this.OnMouseMoveIntoFormClientEvnetHandler));
		}

		private void OnMouseLeaveFormClientEvnetHandler(object sender, EventArgs e)
		{
			System.Windows.Forms.Form userForm = this.m_UserForm;
		}

		private void OnMouseMoveIntoFormClientEvnetHandler(object sender, EventArgs e)
		{
			System.Windows.Forms.Form userForm = this.m_UserForm;
		}

		public new XYZ PickEndPoint(XYZ ptStart, string strPrompt = "请确定直线终点：")
		{
			try
			{
				base.JigStage = PELineJig.EStage.eEndPt;
				base.StartPoint = ptStart;
				base.EndPoint = base.UiDoc.Selection.PickPoint(strPrompt);
				base.EndPoint = this.GetResetPoint(base.StartPoint, base.EndPoint);
			}
			catch
			{
				if (this.m_prompDlg != null && base.CmdEndType == XUiJigBase.ECmdEndType.eEnter)
				{
					base.CmdEndType = XUiJigBase.ECmdEndType.eOther;
					XYZ xyz = base.Client2Revit(this.m_ptCur);
					xyz = this.GetResetPoint(base.StartPoint, xyz);
					double num = Convert.ToDouble(this.m_prompDlg.GetInputValue()) / 304.8;
					XYZ xyz2 = (xyz - base.StartPoint).Normalize();
					base.EndPoint = base.StartPoint + xyz2 * num;
				}
				else
				{
					base.EndPoint = null;
				}
			}
			base.JigStage = PELineJig.EStage.eNoOper;
			return base.EndPoint;
		}

		public new XYZ GetResetPoint(XYZ ptBase, XYZ ptUser)
		{
			XYZ result = ptUser;
			if (base.ResetPoint == XUiJigBase.EResetPoint.eByAngle)
			{
				result = JigFuncs.GetProjectPoint(base.UiDoc, ptUser, ptBase, base.LineAngle);
			}
			else if (base.ResetPoint == XUiJigBase.EResetPoint.eByOrtho)
			{
				result = JigFuncs.GetOrthoPoint(base.UiDoc, ptUser, ptBase);
			}
			else if (base.ResetPoint == XUiJigBase.EResetPoint.eByAxis)
			{
				XYZ axisPoint = JigFuncs.GetAxisPoint(base.UiDoc, ptUser, ptBase);
				if (axisPoint != null)
				{
					result = axisPoint;
				}
			}
			return result;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			if (!this.m_ptCur.IsEmpty && paintEventArgs != null && base.JigStage == PELineJig.EStage.eEndPt)
			{
				XYZ xyz = base.Client2Revit(this.m_ptCur);
				XYZ xyz2 = xyz;
				if (base.ResetPoint == XUiJigBase.EResetPoint.eByAngle)
				{
					XYZ projectPoint = JigFuncs.GetProjectPoint(base.UiDoc, xyz, base.StartPoint, base.LineAngle);
					this.m_ptCur = base.Revit2Client(projectPoint);
				}
				else if (base.ResetPoint == XUiJigBase.EResetPoint.eByOrtho)
				{
					XYZ orthoPoint = JigFuncs.GetOrthoPoint(base.UiDoc, xyz, base.StartPoint);
					this.m_ptCur = base.Revit2Client(orthoPoint);
				}
				else if (base.ResetPoint == XUiJigBase.EResetPoint.eByAxis)
				{
					XYZ axisPoint = JigFuncs.GetAxisPoint(base.UiDoc, xyz, base.StartPoint);
					if (axisPoint != null)
					{
						xyz2 = axisPoint;
					}
				}
				if (!base.StartPoint.IsAlmostEqualTo(new XYZ(xyz2.X, xyz2.Y, base.StartPoint.Z)))
				{
					this.resultLines = new List<Line>
					{
						DrawMultiPipeUtility.GetParallelLineByDistance(base.StartPoint, new XYZ(xyz2.X, xyz2.Y, base.StartPoint.Z), -this.offset[0])
					};
					if (!this.gasLiquidIntegration)
					{
						this.resultLines.Add(DrawMultiPipeUtility.GetParallelLineByDistance(base.StartPoint, new XYZ(xyz2.X, xyz2.Y, base.StartPoint.Z), -this.offset[1]));
					}
					XGraphics g = new XGraphics(paintEventArgs.Graphics, base.JigPen);
					foreach (Line curve in this.resultLines)
					{
						new XLine2D(base.Revit2Client(curve.GetEndPoint(0)), base.Revit2Client(curve.GetEndPoint(1))).Draw(g);
					}
				}
			}
		}

		private System.Drawing.Point Revit2Client()
		{
			throw new NotImplementedException();
		}

		private List<Line> resultLines = new List<Line>();

		private bool gasLiquidIntegration;

		private List<double> offset;
	}
}
